package com.ndp.fb.fbagent.service.impl;

import java.util.Arrays;
import java.util.List;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.fbagent.service.ProductFeedFBService;
import com.ndp.fb.model.fbagent.param.product.FbFeedParam;
import com.ndp.fb.model.fbagent.result.productfeed.FBProductFeed;
import com.restfb.Parameter;

/**
 * Created by Elvis on 2016/4/14.
 */
@Service
public class ProductFeedFBServiceImpl extends AbstractFBagentService implements ProductFeedFBService {

    @Override
    public void fetchFeed(FbFeedParam param) {
        String fields = "id,country,created_time,default_currency,deletion_enabled,delimiter,encoding,file_name,name,product_count,schedule,latest_upload,uploads{errors{severity,summary,description,total_count},id}";
        param.setFields(fields);
        String url = "/"+param.getProductCatalogId()+"/product_feeds";
        Parameter[] parameters = this.adapterArray(param);
        makeGetRequest(url, param.getAccessToken(), parameters);
    }
    @Override
    public List<FBProductFeed> fetchByCatalogSync(FbFeedParam param) {
        String fields = "id,country,created_time,default_currency,deletion_enabled,delimiter,encoding,file_name,name,product_count,schedule,latest_upload,uploads{errors{severity,summary,description,total_count},id}";
        param.setFields(fields);
        String url = "/"+param.getProductCatalogId()+"/product_feeds";
        Parameter[] parameters = this.adapterArray(param);
        return syncFetchFacebookResult(url, param.getAccessToken(), Arrays.asList(parameters), true,0,100,FBProductFeed.class).getObjects();
    }



    @Override
    public void sendToFB(FbFeedParam param) {
        String url = "/" + param.getProductCatalogId() + "/product_feeds";
        Parameter[] parameters = this.adapterArray(param);
        makePostRequest(url, param.getAccessToken(), parameters);
    }

    @Override
    public void fetchNowFeed(FbFeedParam param) {
        String url = "/" + param.getFbFeedId() + "/fetch";
        Parameter[] parameters = this.adapterArray(param);
        makePostRequest(url, param.getAccessToken(), parameters);
    }


    @Override
    public void updateFeed(FbFeedParam param) {
        String url = "/" + param.getFbFeedId();
        Parameter[] parameters = this.adapterArray(param);
        makePostRequest(url, param.getAccessToken(), parameters);
    }

    @Override
    public void drop(FbFeedParam param) {
        String url = "/" + param.getFbFeedId();
        makeDeleteRequest(url,param.getAccessToken());
    }
}
